using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Forms;


namespace Shraga.Client
{
	public class ISTListView2 : System.Windows.Forms.ListView
	{
		private DataTable datatable;
		private DataView dataview;
		private string displaymember;
		private string valuemember;
		private System.ComponentModel.IContainer components = null;

		public ISTListView2()
		{
			// This call is required by the Windows Form Designer.
			InitializeComponent();			
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if (components != null) 
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
			components = new System.ComponentModel.Container();
		}
		#endregion
	

/*		[Category("Data")]
		[Description("Whether the button stays pressed.")]
		[RefreshProperties(RefreshProperties.Repaint)]
		[DefaultValue(null)]							*/
		public DataView DataSource
		{						
			set
			{
				//create a Deep copy of the datatable
				BinaryFormatter formatter = new BinaryFormatter();								
				MemoryStream formatterstream = new MemoryStream();
				formatter.Serialize(formatterstream,value.Table);
				formatterstream.Seek(0,System.IO.SeekOrigin.Begin);
				datatable = ((DataTable)formatter.Deserialize(formatterstream));
				
				//create copy of the view
				dataview = new DataView();
				dataview.Table = datatable;
				dataview.RowFilter = value.RowFilter;
				dataview.RowStateFilter = value.RowStateFilter;
				dataview.Sort = value.Sort;
			}
		}

		public string DisplayMember
		{			
			set{displaymember = value;}
		}

		public string ValueMember
		{
			set{valuemember = value;}
		}

		public int SelectedValue
		{			
			get{return ValueOfAMember(this.FocusedItem);}
		}

		public int ValueByIndex(int Index)
		{
			return ValueOfAMember(this.Items[Index]);
		}
		

		public void Populate()
		{
			this.Items.Clear();
			if ((dataview != null) && (displaymember != null) && (valuemember != null))
			{
				foreach(DataRowView drv in dataview)
				{
					ListViewItem TempListViewItem = new ListViewItem();
					TempListViewItem.Text = drv[displaymember].ToString();
					TempListViewItem.Tag = drv[valuemember].ToString();
					this.Items.Add(TempListViewItem);
					
				}
			}
		}

		private int ValueOfAMember(ListViewItem Item)
		{
			/*
				string selecteditem =  Item.Text;

				for(int i = 0; i < dataview.Count;i++)
				{
				if (dataview[i][displaymember].ToString() == selecteditem)
				return Convert.ToInt32(dataview[i][valuemember].ToString());					
				}*/

			if (Item!=null)
			return Convert.ToInt32(Item.Tag);
			else
			return -1;
		}

		#region Mirroring Stuff

		const int WS_EX_LAYOUTRTL = 0x400000;
		private bool _mirrored= false;
		[Description("Change to the right-to-left layout."),DefaultValue(false), 
		Localizable(true),Category("Appearance"),Browsable(true)] 
		public bool Mirrored
		{
			get
			{
				return _mirrored;
			}
			set
			{
				if (_mirrored != value) 
				{
					_mirrored = value;
					base.OnRightToLeftChanged(EventArgs.Empty);
				}
			}
		}
	
		protected override CreateParams CreateParams
		{
			get
			{
				if (_mirrored==true)
				{
					CreateParams CP;
					CP = base.CreateParams;
					CP.ExStyle = CP.ExStyle | WS_EX_LAYOUTRTL ;
					return CP;
				}
				else return base.CreateParams;
			}
		}
		
		#endregion

	}
}

